<?php

namespace HelloAdmin\Controller;

class RoleController extends CommonController {

    private $Role;

    public function __construct() {
        parent::__construct();
        $this->Role = D('Role');
    }

    public function index() {
        


        $count = $this->Role->GetCount($where);
        parent::backPage($count);
        $data = $this->Role->GetDataByWhere($where, '', $this->offset, $this->numPerPage, $fields = '');
        $this->assign('Count', $count);

        $this->assign('data', $data[0]);
        $this->display();
    }
    
    public function roleUser(){
        
        $data = D('User')->GetDataByWhere('','','','','username,user_id', $hasCount = false, $bigData = FALSE);
        
        $user = M('RoleUser')->where("`role_id` = '".I('get.role_id')."'")->field('user_id')->select();
        if($user){
            $users = array();
            foreach ($user as $val){
                $users[] = $val['user_id'];
            }
        }
        $this->assign('data',$data[0]);
        $this->assign('users',$users);
        $this->display();
    }
    
    public function userRole(){
        $post = $this->GetParam('role_id,user_id');
        //var_dump($post);exit;
        if($post['role_id'] > 0){
            $Roleuser =  M('RoleUser');
            $Roleuser->where("role_id = {$post['role_id']}")->delete();
            if(!empty($post['user_id'])){
                foreach($post['user_id'] as $val){
                    $data = array('role_id'=>$post['role_id'],'user_id'=> $val);
                    //var_dump($data);
                    $Roleuser->add($data);
                }
            } 
            $this->success('授权成功');exit;
        }
        $this->error('授权失败');
        
    }
    
    public function nodeRole(){
        $data = D('Node')->getlist();
        //var_dump($data);exit;
        $node = M('RoleNode')->where("`role_id` = '".I('get.role_id')."'")->field('node_id')->select();
        if($node){
            $nodes = array();
            foreach ($nodes as $val){
                $nodes[] = $val['node_id'];
            }
        }
        $this->assign('data',$data);
        $this->assign('nodes',$nodes);
        $this->display();
        
    }
    
}
